﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

/// <summary>
///  访问对像范型抽像类;
/// </summary>
/// 
namespace QzEbook.Common
{
    public abstract class abstractAccess<T> 
    {
        public abstractAccess()
        {
        }

        /// <summary>
        /// 创建通用数据访问对像;
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DbCommand CreateCommand(string sql)
        {
            DbCommand command = DataAccess.CreateCommand();
            command.CommandText = sql;
            return command;
        }

        /// <summary>
        /// 绑定默认参数;
        /// </summary>
        /// <param name="command"></param>
        protected virtual void bindDefaultParameter(DbCommand command)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 只输入存储过程参数,;
        /// </summary>
        /// <param name="command"></param>
        /// <param name="name"></param>
        /// <param name="data"></param>
        public void addParameter(DbCommand command, string name, object data)
        {
            addParameter(command, name, data, ParameterDirection.Input);
        }

        /// <summary>
        /// 新增存储过程参数;
        /// </summary>
        /// <param name="command"></param>
        /// <param name="name"></param>
        /// <param name="data"></param>
        /// <param name="direction"></param>
        public void addParameter(DbCommand command, string name, object data, ParameterDirection direction)
        {
            DbParameter parameter = command.CreateParameter();
            parameter.ParameterName = name;
            parameter.Value = data;
            parameter.Direction = direction;
            command.Parameters.Add(parameter);
        }

        /// <summary>
        /// 用于分页的通用存储过程;
        /// </summary>
        /// <param name="sortat">主键</param>
        /// <param name="some">选取列名</param>
        /// <param name="from">表名</param>
        /// <param name="where">条件</param>
        /// <param name="order">排序</param>
        /// <param name="index">当前页</param>
        /// <param name="size">每页个数</param>
        /// <param name="rows">总数</param>
        /// <param name="pages">总页数</param>
        /// <returns></returns>
        public DataTable selectPagination(string sortat,string some,string from,string where,string order,int index,int size,ref int rows,ref int pages )
        {
            DbCommand command=CreateCommand("PaginationStore");
            addParameter(command, "@FdName", sortat);
            addParameter(command, "@PageSize", size);
            addParameter(command, "@SelectStr", some);
            addParameter(command, "@FromStr", from);
            addParameter(command, "@OrderByStr", order);
            addParameter(command, "@WhereStr", where);
            addParameter(command, "@PageCurrent", index);
            addParameter(command, "@CountRows", rows, ParameterDirection.Output);
            addParameter(command, "@CountPage", pages, ParameterDirection.Output);
            DataTable table=DataAccess.ExcuteSelectCommand(command);
            rows =(int)command.Parameters["@CountRows"].Value;
            pages = (int)command.Parameters["@CountPage"].Value;

            return table;
        }
        

        /// <summary>
        /// 绑定整表;
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public List<T> bindTable(DataTable table)
        {
            List<T> list = new List<T>(table.Rows.Count);
            foreach (DataRow row in table.Rows)
            {
                T t=this.bindRow(row);
                list.Add(t);
            }
            return list;
        }

        /// <summary>
        /// 选取第一条记录;
        /// </summary>
        /// <returns></returns>
        public T select()
        {
            DataTable table = this.selectTable();
            if (table.Rows.Count < 1) return default(T);
            return bindRow(table.Rows[0]);
        }
        /// <summary>
        /// 取出范型数组;
        /// </summary>
        /// <returns></returns>
        public virtual List<T> selectList()
        {
            DataTable table=this.selectTable();
            return bindTable(table);
        }

        /// <summary>
        /// 下面几个不一定要实现的;
        /// </summary>
        /// <returns></returns>
        public virtual bool insert(){
            throw new NotImplementedException();
        }
        public virtual bool delete()
        {
            throw new NotImplementedException();
        }
        public virtual bool update()
        {
            throw new NotImplementedException();
        }



        /// <summary>
        /// 由子类实现具体的类属性绑定;
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        protected abstract T bindRow(DataRow row);
        /// <summary>
        /// 由子类实现具体的数据表选取;
        /// </summary>
        /// <returns></returns>
        protected abstract DataTable selectTable();
    }
}